home *** CD-ROM | disk | FTP | other *** search
/ Precision Software Appli…tions Silver Collection 1 / Precision Software Applications Silver Collection Volume One (PSM) (1993).iso / windows / games / wmfread.arj / WMFDCODE.C < prev    next >
C/C++ Source or Header  |  1992-11-17  |  18KB  |  505 lines

  1. /****************************************************************************
  2.  
  3.     PROGRAM: wmfdcode.c
  4.  
  5.     PURPOSE: view and decode Windows Metafiles
  6.  
  7.     FUNCTIONS:
  8.  
  9.     WinMain() - calls initialization function, processes message loop
  10.     InitApplication() - initializes window data and registers window
  11.     InitInstance() - saves instance handle and creates main window
  12.     MainWndProc() - processes messages
  13.     WaitCursor() - loads hourglass cursor/restores original cursor
  14.  
  15.     HISTORY: 1/16/91 - wrote it - drc
  16.  
  17. ****************************************************************************/
  18. // COPYRIGHT:
  19. //
  20. //   (C) Copyright Microsoft Corp. 1992.  All rights reserved.
  21. //
  22. //   You have a royalty-free right to use, modify, reproduce and
  23. //   distribute the Sample Files (and/or any modified version) in
  24. //   any way you find useful, provided that you agree that
  25. //   Microsoft has no warranty obligations or liability for any
  26. //   Sample Application Files which are modified.
  27. #define MAIN
  28. #include "windows.h"                /* required for all Windows applications */
  29. #include "wmfdcode.h"               /* specific to this program          */
  30.  
  31.  
  32. /***********************************************************************
  33.  
  34.   FUNCTION   : WinMain
  35.  
  36.   PARAMETERS : HANDLE
  37.            HANDLE
  38.            LPSTR
  39.            int
  40.  
  41.   PURPOSE    : calls initialization function, processes message loop
  42.  
  43.   CALLS      : WINDOWS
  44.          GetMessage
  45.          TranslateMessage
  46.          DispatchMessage
  47.  
  48.            APP
  49.          InitApplication
  50.  
  51.   RETURNS    : int
  52.  
  53.   COMMENTS   : Windows recognizes this function by name as the initial entry
  54.            point for the program.  This function calls the application
  55.            initialization routine, if no other instance of the program is
  56.            running, and always calls the instance initialization routine.
  57.            It then executes a message retrieval and dispatch loop that is
  58.            the top-level control structure for the remainder of execution.
  59.            The loop is terminated when a WM_QUIT message is received, at
  60.            which time this function exits the application instance by
  61.            returning the value passed by PostQuitMessage().
  62.  
  63.            If this function must abort before entering the message loop,
  64.            it returns the conventional value NULL.
  65.  
  66.   HISTORY    : 1/16/91 - created - modified from SDK sample GENERIC
  67.  
  68. ************************************************************************/
  69.  
  70.  
  71. int PASCAL WinMain (hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  72. HANDLE hInstance;                            /* current instance             */
  73. HANDLE hPrevInstance;                        /* previous instance            */
  74. LPSTR lpCmdLine;                             /* command line                 */
  75. int nCmdShow;                                /* show-window type (open/icon) */
  76. {
  77.    MSG msg;                                 /* message                      */
  78.  
  79.    if (!hPrevInstance)                  /* Other instances of app running? */
  80.       if (!InitApplication(hInstance)) /* Initialize shared things */
  81.          return (FALSE);              /* Exits if unable to initialize     */
  82.  
  83.    /* Perform initializations that apply to a specific instance */
  84.    if (!InitInstance(hInstance, nCmdShow))
  85.       return (FALSE);
  86.  
  87.    /* Acquire and dispatch messages until a WM_QUIT message is received. */
  88.    while (GetMessage(&msg,        /* message structure                      */
  89.                      NULL,                  /* handle of window receiving the message */
  90.                      NULL,                  /* lowest message to examine              */
  91.                      NULL))                 /* highest message to examine             */
  92.    {
  93.       TranslateMessage(&msg);    /* Translates virtual key codes           */
  94.       DispatchMessage(&msg);     /* Dispatches message to window           */
  95.    }
  96.    return (msg.wParam);           /* Returns the value from PostQuitMessage */
  97. }
  98.  
  99.  
  100. /***********************************************************************
  101.  
  102.   FUNCTION   : InitApplication
  103.  
  104.   PARAMETERS : HANDLE hInstance
  105.  
  106.   PURPOSE    : Initializes window data and registers window class
  107.  
  108.   CALLS      : WINDOWS
  109.          RegisterClass
  110.  
  111.   MESSAGES   : none
  112.  
  113.   RETURNS    : BOOL
  114.  
  115.   COMMENTS   : This function is called at initialization time only if no
  116.            other instances of the application are running.  This function
  117.            performs initialization tasks that can be done once for any
  118.            number of running instances.
  119.  
  120.            In this case, we initialize a window class by filling out a
  121.            data structure of type WNDCLASS and calling the Windows
  122.            RegisterClass() function.  Since all instances of this
  123.            application use the same window class, we only need to do this
  124.            when the first instance is initialized.
  125.  
  126.   HISTORY    : 1/16/91 - created - modified from SDK sample app GENERIC
  127.  
  128. ************************************************************************/
  129.  
  130.  
  131. BOOL InitApplication (hInstance)
  132. HANDLE hInstance;                       /* current instance */
  133. {
  134.    WNDCLASS wc;
  135.  
  136.    bInPaint = FALSE;
  137.  
  138.    /* Fill in window class structure with parameters that describe the
  139.       main window. */
  140.    wc.style = NULL;                    /* Class style(s) */
  141.    wc.lpfnWndProc = MainWndProc;       /* Function to retrieve messages for */
  142.    /* windows of this class */
  143.    wc.cbClsExtra = 0;                  /* No per-class extra data */
  144.    wc.cbWndExtra = 0;                  /* No per-window extra data */
  145.    wc.hInstance = hInstance;           /* Application that owns the class */
  146.    wc.hIcon = LoadIcon(hInstance, "WMFICON");
  147.    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  148.    wc.hbrBackground = COLOR_WINDOW + 1;
  149.    wc.lpszMenuName = "MetaMenu";      /* Name of menu resource in .RC file */
  150.    wc.lpszClassName = "MetaWndClass";  /* Name used in call to CreateWindow */
  151.  
  152.    /* Register the window class and return success/failure code */
  153.    return (RegisterClass(&wc));
  154. }
  155.  
  156. /***********************************************************************
  157.  
  158.   FUNCTION   : InitInstance
  159.  
  160.   PARAMETERS : HANDLE  hInstance - Current instance identifier
  161.            int     nCmdShow  - Param for first ShowWindow() call
  162.  
  163.   PURPOSE    : Saves instance handle and creates main window
  164.  
  165.   CALLS      : WINDOWS
  166.          CreateWindow
  167.          ShowWindow
  168.          UpdateWindow
  169.  
  170.   MESSAGES   : none
  171.  
  172.   RETURNS    : BOOL
  173.  
  174.   COMMENTS   : This function is called at initialization time for every
  175.            instance of this application.  This function performs
  176.            initialization tasks that cannot be shared by multiple
  177.            instances.
  178.  
  179.            In this case, we save the instance handle in a static variable
  180.            and create and display the main program window.
  181.  
  182.   HISTORY    :
  183.  
  184. ************************************************************************/
  185.  
  186.  
  187. BOOL InitInstance (hInstance, nCmdShow)
  188. HANDLE hInstance;          /* Current instance identifier.       */
  189. int nCmdShow;           /* Param for first ShowWindow() call. */
  190. {
  191.    HWND hWnd;               /* Main window handle.                */
  192.  
  193.    /* Save the instance handle in static variable, which will be used in  */
  194.    /* many subsequence calls from this application to Windows.            */
  195.  
  196.    hInst = hInstance;
  197.  
  198.    /* Create a main window for this application instance.  */
  199.    hWnd = CreateWindow("MetaWndClass",                 /* See RegisterClass() call.          */
  200.                        APPNAME,                        /* Text for window title bar.         */
  201.                        WS_OVERLAPPEDWINDOW,            /* Window style.                      */
  202.                        CW_USEDEFAULT,                  /* Default horizontal position.       */
  203.                        CW_USEDEFAULT,                  /* Default vertical position.         */
  204.                        CW_USEDEFAULT,                  /* Default width.                     */
  205.                        CW_USEDEFAULT,                  /* Default height.                    */
  206.                        NULL,                           /* Overlapped windows have no parent. */
  207.                        NULL,                           /* Use the window class menu.         */
  208.                        hInstance,                      /* This instance owns this window.    */
  209.                        NULL                            /* Pointer not needed.                */
  210.                        );
  211.  
  212.    /* If window could not be created, return "failure" */
  213.    if (!hWnd)
  214.       return (FALSE);
  215.    hWndMain = hWnd;
  216.  
  217.    /* Make the window visible; update its client area; and return "success" */
  218.    ShowWindow(hWnd, nCmdShow);  /* Show the window                        */
  219.    UpdateWindow(hWnd);          /* Sends WM_PAINT message                 */
  220.    return (TRUE);               /* Returns the value from PostQuitMessage */
  221. }
  222.  
  223. /***********************************************************************
  224.  
  225.   FUNCTION   : MainWndProc
  226.  
  227.   PARAMETERS : HWND hWnd        -  window handle
  228.            unsigned message -  type of message
  229.            WORD wParam      -  additional information
  230.            LONG lParam      -  additional information
  231.  
  232.   PURPOSE    : Processes messages
  233.  
  234.   CALLS      :
  235.  
  236.   MESSAGES   : WM_CREATE
  237.  
  238.            WM_COMMAND
  239.  
  240.          wParams
  241.          - IDM_EXIT
  242.          - IDM_ABOUT
  243.          - IDM_OPEN
  244.          - IDM_PRINT
  245.          - IDM_LIST
  246.          - IDM_CLEAR
  247.          - IDM_ENUM
  248.          - IDM_ENUMRANGE
  249.          - IDM_ALLREC
  250.          - IDM_DESTDISPLAY
  251.          - IDM_DESTMETA
  252.          - IDM_HEADER
  253.          - IDM_CLIPHDR
  254.          - IDM_ALDUSHDR
  255.  
  256.            WM_DESTROY
  257.  
  258.   RETURNS    : long
  259.  
  260.   COMMENTS   :
  261.  
  262.   HISTORY    : 1/16/91 - created - drc
  263.  
  264. ************************************************************************/
  265.  
  266.  
  267. long FAR PASCAL MainWndProc (hWnd, message, wParam, lParam)
  268. HWND hWnd;                                /* window handle                   */
  269. UINT message;                         /* type of message         */
  270. WPARAM wParam;                              /* additional information           */
  271. LPARAM lParam;                              /* additional information           */
  272. {
  273.    RECT rect;
  274.    int iFOpenRet;
  275.    char TempOpenName[128];
  276.    int iDlgRet;
  277.  
  278.    switch (message)
  279.       {
  280.    case WM_CREATE:
  281.  
  282.       /* init the state of the menu items */
  283.       EnableMenuItem(GetMenu(hWnd), IDM_VIEW, MF_DISABLED | MF_GRAYED |
  284.                      MF_BYPOSITION);
  285.       EnableMenuItem(GetMenu(hWnd), IDM_PLAY, MF_DISABLED | MF_GRAYED |
  286.                      MF_BYPOSITION);
  287.       EnableMenuItem(GetMenu(hWnd), IDM_PRINT, MF_DISABLED | MF_GRAYED);
  288.       CheckMenuItem(GetMenu(hWnd), IDM_DESTDISPLAY, MF_CHECKED);
  289.       break;
  290.  
  291.    case WM_COMMAND:
  292.       /* message: command from application menu */
  293.       switch (wParam)
  294.          {
  295.       case IDM_EXIT: /* file exit menu option */
  296.          PostQuitMessage(0);
  297.          break;
  298.  
  299.       case IDM_ABOUT: /* about box */
  300.          lpProcAbout = MakeProcInstance(About, hInst);
  301.          DialogBox(hInst,                      /* current instance         */
  302.                    "AboutBox",                  /* resource to use          */
  303.                    hWnd,                       /* parent handle            */
  304.                    lpProcAbout);               /* About() instance address */
  305.          FreeProcInstance(lpProcAbout);
  306.          break;
  307.  
  308.       case IDM_OPEN: /* select a metafile to open */
  309.  
  310.          /* save the name of previously opened file */
  311.          if (lstrlen((LPSTR)OpenName) != 0)
  312.             lstrcpy((LPSTR)TempOpenName, (LPSTR)OpenName);
  313.  
  314.          /* initialize file info flags */
  315.          if (!bMetaFileOpen)
  316.          {
  317.             bBadFile = FALSE;
  318.             bValidFile = FALSE;
  319.          }
  320.  
  321.          /* clear the client area */
  322.          GetClientRect(hWnd, (LPRECT)&rect);
  323.          InvalidateRect(hWnd, (LPRECT)&rect, TRUE);
  324.  
  325.          /* call file open dlg */
  326.          iFOpenRet = OpenFileDialog((LPSTR)OpenName);
  327.  
  328.          /* if a file was selected */
  329.          if (iFOpenRet)
  330.          {
  331.  
  332.             /* if file contains a valid metafile and it was rendered */
  333.             if (!ProcessFile(hWnd, (LPSTR)OpenName))
  334.                lstrcpy((LPSTR)OpenName, (LPSTR)TempOpenName);
  335.          }
  336.          else
  337.             lstrcpy((LPSTR)OpenName, (LPSTR)TempOpenName);
  338.          break;
  339.  
  340.       case IDM_PRINT: /* play the metafile to a printer DC */
  341.  
  342.          /* if the metafile hasn't already been rendered as a placeable
  343.         or clipboard metafile */
  344.          if (!bMetaInRam)
  345.             hMF = GetMetaFile((LPSTR)OpenName);
  346.  
  347.          /* print it */
  348.          PrintWMF();
  349.          break;
  350.  
  351.       case IDM_LIST: /* list box containing all records of metafile */
  352.          WaitCursor(TRUE);
  353.          lpListDlgProc = MakeProcInstance(ListDlgProc, hInst);
  354.          DialogBox(hInst,             /* current instance         */ "LISTRECS"
  355.                    ,                         /* resource to use          */
  356.                    hWnd,                      /* parent handle            */
  357.                    lpListDlgProc);            /* About() instance address */
  358.          FreeProcInstance(lpListDlgProc);
  359.          WaitCursor(FALSE);
  360.          break;
  361.  
  362.       case IDM_CLEAR: /* clear the client area */
  363.          GetClientRect(hWnd, (LPRECT)&rect);
  364.          InvalidateRect(hWnd, (LPRECT)&rect, TRUE);
  365.          break;
  366.  
  367.       case IDM_ENUM: /* play - step - all menu option */
  368.  
  369.          /* set flags appropriately before playing to destination */
  370.          bEnumRange = FALSE;
  371.          bPlayItAll = FALSE;
  372.          PlayMetaFileToDest(hWnd, iDestDC);
  373.          break;
  374.  
  375.       case IDM_ENUMRANGE: /* play - step - range menu option */
  376.  
  377.          /* odd logic here...this just forces evaluation of the
  378.         enumeration range in MetaEnumProc. We are not "playing
  379.         it all" */
  380.          bPlayItAll = TRUE;
  381.          lpEnumRangeDlg = MakeProcInstance(EnumRangeDlgProc, hInst);
  382.          iDlgRet = DialogBox(hInst, "ENUMRANGE", hWnd, lpEnumRangeDlg);
  383.          FreeProcInstance(lpEnumRangeDlg);
  384.  
  385.          /* if cancel button not pressed, play to destination */
  386.          if (iDlgRet != IDCANCEL)
  387.             PlayMetaFileToDest(hWnd, iDestDC);
  388.          break;
  389.  
  390.       case IDM_ALLREC: /* play - all menu option */
  391.  
  392.          /* set flag appropriately and play to destination */
  393.          bEnumRange = FALSE;
  394.          bPlayItAll = TRUE;
  395.          bPlayRec = TRUE;
  396.          PlayMetaFileToDest(hWnd, iDestDC);
  397.          break;
  398.  
  399.       case IDM_DESTDISPLAY: /* play - destination - display menu option */
  400.          CheckMenuItem(GetMenu(hWnd), IDM_DESTDISPLAY, MF_CHECKED);
  401.          CheckMenuItem(GetMenu(hWnd), IDM_DESTMETA, MF_UNCHECKED);
  402.  
  403.          /* set destination flag to display */
  404.          iDestDC = DESTDISPLAY;
  405.          break;
  406.  
  407.       case IDM_DESTMETA: /* play - destination - metafile menu option */
  408.          CheckMenuItem(GetMenu(hWnd), IDM_DESTDISPLAY, MF_UNCHECKED);
  409.          CheckMenuItem(GetMenu(hWnd), IDM_DESTMETA, MF_CHECKED);
  410.  
  411.          /* set destination flag to metafile */
  412.          iDestDC = DESTMETA;
  413.          break;
  414.  
  415.       case IDM_HEADER: /* display the common metafile header */
  416.          if (bValidFile)
  417.          {
  418.             lpHeaderDlg = MakeProcInstance(HeaderDlgProc, hInst);
  419.             DialogBox(hInst, "HEADER", hWnd, lpHeaderDlg);
  420.             FreeProcInstance(lpHeaderDlg);
  421.          }
  422.          break;
  423.  
  424.       case IDM_CLIPHDR: /* display the metafilepict of a clipboard file */
  425.          if (bValidFile)
  426.          {
  427.             lpClpHeaderDlg = MakeProcInstance(ClpHeaderDlgProc, hInst);
  428.             DialogBox(hInst,            /* current instance         */
  429.                       "CLIPHDR",                         /* resource to use          */
  430.                       hWnd,                     /* parent handle            */
  431.                       lpClpHeaderDlg);          /* About() instance address */
  432.             FreeProcInstance(lpClpHeaderDlg);
  433.          }
  434.          break;
  435.  
  436.       case IDM_ALDUSHDR: /* display the placeable metafile header */
  437.          if (bValidFile)
  438.          {
  439.             lpAldusHeaderDlg = MakeProcInstance(AldusHeaderDlgProc, hInst);
  440.             DialogBox(hInst,            /* current instance         */
  441.                       "ALDUSHDR",                        /* resource to use          */
  442.                       hWnd,                     /* parent handle            */
  443.                       lpAldusHeaderDlg);                /* About() instance address */
  444.             FreeProcInstance(lpAldusHeaderDlg);
  445.          }
  446.          break;
  447.  
  448.       default:  /* let Windows process it */
  449.          return (DefWindowProc(hWnd, message, wParam, lParam));
  450.          }
  451.       break;
  452.  
  453.    case WM_DESTROY: /* message: window being destroyed */
  454.       PostQuitMessage(0);
  455.       break;
  456.  
  457.    default:  /* passes it on if unproccessed */
  458.       return (DefWindowProc(hWnd, message, wParam, lParam));
  459.       }
  460.    return (NULL);
  461. }
  462.  
  463. /***********************************************************************
  464.  
  465.   FUNCTION   : WaitCursor
  466.  
  467.   PARAMETERS : BOOL bWait - TRUE for the hour glass cursor
  468.                 FALSE to return to the previous cursor
  469.  
  470.   PURPOSE    : toggle the mouse cursor to the hourglass and back
  471.  
  472.   CALLS      : WINDOWS
  473.          LoadCursor
  474.          SetCursor
  475.  
  476.   MESSAGES   : none
  477.  
  478.   RETURNS    : void
  479.  
  480.   COMMENTS   :
  481.  
  482.   HISTORY    : 1/16/91 - created - drc
  483.  
  484. ************************************************************************/
  485.  
  486.  
  487. void WaitCursor (bWait)
  488. BOOL bWait;
  489. {
  490.    HCURSOR hCursor;
  491.    static HCURSOR hOldCursor;
  492.  
  493.    /* if hourglass cursor is to be used */
  494.  
  495.    if (bWait)
  496.    {
  497.       hCursor = LoadCursor(NULL, IDC_WAIT);
  498.       hOldCursor = SetCursor(hCursor);
  499.    }
  500.    else
  501.    {
  502.       SetCursor(hOldCursor);
  503.    }
  504. }
  505.